/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2005  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/

#include "HookNet.h"
#include "SpyDllInt.h"
#include "Hooks.h"


int WINAPI Spy_connect (SOCKET s, const struct sockaddr* name, int namelen)
{
  DWORD	nReaction ;
  struct sockaddr_in * sin = (struct sockaddr_in *) name ;
  PROC	pfnStub = Hooks_GetStubAddress (HOOKS_CONNECT) ;
  TCHAR szAddress[32] ;

  // log call
  wsprintf (szAddress, TEXT("%hu.%hu.%hu.%hu"), 
	    sin->sin_addr.S_un.S_un_b.s_b1, sin->sin_addr.S_un.S_un_b.s_b2, 
	    sin->sin_addr.S_un.S_un_b.s_b3, sin->sin_addr.S_un.S_un_b.s_b4) ;
  nReaction = SpyInt_CatchCall (REASON_NET_CONNECT, TEXT("sn"), szAddress, ntohs(sin->sin_port)) ;

  if( nReaction != RULE_ACCEPT ) return SOCKET_ERROR ;

  return pfnStub (s, name, name) ;
}



int WINAPI Spy_listen (SOCKET s, int backlog)
{
  DWORD			nReaction ;
  struct sockaddr_in	sin ;
  int			sin_len;
  PROC			pfnStub = Hooks_GetStubAddress (HOOKS_LISTEN) ;
  TCHAR			szAddress[32] ;

  // get socket address
  getsockname (s, (struct sockaddr*)&sin, &sin_len);

  // log call
  wsprintf (szAddress, TEXT("%hu.%hu.%hu.%hu"), 
	    sin.sin_addr.S_un.S_un_b.s_b1, sin.sin_addr.S_un.S_un_b.s_b2, 
	    sin.sin_addr.S_un.S_un_b.s_b3, sin.sin_addr.S_un.S_un_b.s_b4) ;
  nReaction = SpyInt_CatchCall (REASON_NET_LISTEN, TEXT("sn"), szAddress, ntohs(sin.sin_port)) ;

  if( nReaction == RULE_FEIGN ) return 0 ;
  if( nReaction == RULE_REJECT ) return SOCKET_ERROR ;

  return pfnStub (s, backlog) ;
}
